var baseURL = "../";
var designation = "";
$(function () {
    selectDesignation();
    $('.Typeshow').click(function () {
        var $t = $(this);
        if ($t.find("i").hasClass('fa-caret-up')) {
            $('.downlist').hide();
            $t.find("i").removeClass("fa-caret-up").addClass('fa-caret-down');

        } else {
            $(".Typeshow").removeClass('up');
            $('.downlist').hide();
            $t.addClass('up');
            $('.downlist').eq($(".Typeshow").index($(this)[0])).show();
            $(".fa").removeClass("fa-caret-up").addClass('fa-caret-down')
            $t.find("i").removeClass("fa-caret-down").addClass('fa-caret-up');

            //清除下拉选项中所有的选中class并重新添加
            $(".xm ul li a").removeClass('selected');
            $(".jy ul li a").removeClass('selected');
            $(".dq ul li a").removeClass('selected');
            $(".xm ul li a:contains('" + $('#xm span').text() + "')").addClass('selected');
            $(".jy ul li a:contains('" + $('#jy span').text() + "')").addClass('selected');
            $(".dq ul li a:contains('" + $('#dq span').text() + "')").addClass('selected');

            //下拉个数多的情况
            var Listlength = $(".downlist");
            for (var i = 0; i < Listlength.length; i++) {
                if ($(Listlength[i]).children().length > 8) {
                    $(Listlength[i]).css("height", "460px")
                }

            }
        }
    });
    $('#fullName').bind('change', function () {
        //获取input 元素,并实时监听用户输入
        //逻辑
        var fullName = $("#fullName").val()
        selectDesignationDate(fullName)
    });
    /*  $(".tel_telephone").click(function () {
          layer.msg('ashjk')
          layer.msg(this.val())
          window.location.href = "tel:" + this.val()
      })*/
    $.fn.autoHeight = function () {

        function autoHeight(elem) {
            elem.style.height = 'auto';
            elem.scrollTop = 0; //防抖动
            elem.style.height = elem.scrollHeight + 'px';
        }

        this.each(function () {
            autoHeight(this);
            $(this).on('keyup', function () {
                autoHeight(this);
            });
        });

    }


    $('textarea[autoHeight]').autoHeight();
});

function tel_telephone(telephone) {
    window.location.href = "tel:" + telephone
}

/**
 * 加载番号
 */
function selectDesignation() {
    var url = baseURL + "/questionnaire/getDesignationList";
    $.ajax({
        url: url,
        type: "post",
        dataType: "json",
        success: function (data) {
            var messageHtml = "  <li data-id='全部' data-name='全部' onclick='UpDataList(this)'><a href='#'>全部</a></li>";
            for (var i = data.length - 1; i >= 0; i--) {
                messageHtml += "<li data-id='" + data[i] + "' data-name='" + data[i] + "' onclick='UpDataList(this)'><a href='#'>" + data[i] + "</a></li>"
            }
            $("#designationList").html(messageHtml);

        }
    })
}

function UpDataList(thisVal) {
    UpClassCss(thisVal);
    //获取当前元素本身this
    var $t = $(thisVal);
    //获取自定义id和name的值
    var id = $t.attr("data-id");
    var name = $t.attr("data-name");
    if (id == '全部') {
        id = "";
    }
    designation = id;
    var fullName = $("#fullName").val()
    selectDesignationDate(fullName)
    //alert(id+"|"+name);

}

function UpClassCss(thisVal) {
    var $t = $(thisVal);
    var name = $t.attr("data-name");
    //所有的下拉隐藏
    $('.downlist').hide();
    //获取上级元素的class
    var tabId = $t.parent().parent().attr("class");
    //下拉框文字改变
    $("#" + tabId + " span").text(name);
    $(".fa").removeClass("fa-caret-up").addClass('fa-caret-down')
}

function selectDesignationDate(fullName) {
    /*  if(fullName==null||fullName==undefined||fullName==""){
          layer.msg("请输入姓名")
          return;
      }*/
    var url = baseURL + "/questionnaire/selectQuestionnaire?designation=" + designation + "&fullName=" + fullName;
    $.ajax({
        url: url,
        type: "get",
        dataType: "json",
        success: function (dataR) {

            var messageHtml = "";
            if (dataR.code == 0) {
                var data = dataR.list;
                for (var i = data.length - 1; i >= 0; i--) {
                    messageHtml += "<div class='mainCs'>" +
                        "    <div class='mainDivCs'>" +
                        "        <label class='control-label2'>姓名：</label>" +
                        "        <input name=''" +
                        "               placeholder='姓名' maxlength='50'" +
                        " value='" + data[i].fullName + "'              class='input-medium2'/>" +
                        "    </div>" +
                        "    <div class='mainDivCs'>" +
                        "        <label class='control-label2'>番号：</label>" +
                        "        <input name=''" +
                        "               placeholder='番号' maxlength='50'" +
                        " value='" + data[i].designation + "'              class='input-medium2'/>" +
                        "    </div>" +
                        "    <div class='mainDivCs'>" +
                        "        <label class='control-label2'>电话：</label>" +
                        // " <a  href='tel:"+ data[i].telephone +"'>" + data[i].telephone +"</a>"+
                        //   /* href='tel:"+ data[i].telephone +"' */
                        "        <input name=''" +
                        "               placeholder='电话' maxlength='50'" +
                        " value='" + data[i].telephone + "'   style='color: #1c94c4;text-align: left'  type='button' onclick='tel_telephone(" + data[i].telephone + ")'       class='tel_telephone'/>" +
                        "    </div>" +
                        "    <div class='mainDivCs'>" +
                        "        <label class='control-label2'>公司名称：</label>";
                    if (data[i].corporateName != null && data[i].corporateName != "" && data[i].corporateName.length > 14) {
                        messageHtml += "<textarea   rows='' cols='14' autoHeight=\"true\" >" + data[i].corporateName + "</textarea>";
                    } else {
                        messageHtml += "        <input name=''" +
                            "               placeholder='公司名称' maxlength='50'" +
                            "    value='" + data[i].corporateName + "'            class='input-medium2 '/>";
                    }
                    messageHtml += "    </div>" +
                        "    <div class='mainDivCs'>" +
                        "        <label class='control-label2'>行业：</label>";
                    if (data[i].industry != null && data[i].industry != "" && data[i].industry.length > 14) {
                        messageHtml += "<textarea   rows='' cols='14' autoHeight=\"true\" >" + data[i].industry + "</textarea>";
                    } else {
                        messageHtml += "        <input name=''" +
                            "               placeholder='行业' maxlength='50'" +
                            "    value='" + data[i].industry + "'              class='input-medium2 '/>";
                    }
                    messageHtml += "    </div>" +
                        "    <div class='mainDivCs'>" +
                        "        <label class='control-label2'>产品：</label>";
                    if (data[i].product != null && data[i].product != "" && data[i].product.length > 14) {
                        messageHtml += "<textarea   rows='' cols='14' autoHeight=\"true\" >" + data[i].product + "</textarea>";
                    } else {
                        messageHtml += "        <input name=''" +
                            "               placeholder='产品' maxlength='50'" +
                            "    value='" + data[i].product + "'                class='input-medium2 '/>";
                    }

                    messageHtml += "    </div>" +
                        "    <div class='mainDivCs'>" +
                        "        <label class='control-label2'>合作方向：</label>";
                    if (data[i].enterpriseDirection != null && data[i].enterpriseDirection != "" && data[i].enterpriseDirection.length > 14) {
                        messageHtml += "<textarea   rows='' cols='14' autoHeight=\"true\" >" + data[i].enterpriseDirection + "</textarea>";
                    } else {
                        messageHtml += "        <input name=''" +
                            "               placeholder='合作方向' maxlength='50'" +
                            "    value='" + data[i].enterpriseDirection + "'                class='input-medium2 '/>";
                    }
                    messageHtml += "    </div>" +
                        "    <div class='mainDivCs'>" +
                        "        <label class='control-label2'>资源需求：</label>";
                    if (data[i].resourceDemand != null && data[i].resourceDemand != "" && data[i].resourceDemand.length > 14) {
                        messageHtml += "<textarea   rows='' cols='14' autoHeight=\"true\" >" + data[i].resourceDemand + "</textarea>";
                    } else {
                        messageHtml += "  <input name=''" +
                            "               placeholder='资源需求' maxlength='50'" +
                            "    value='" + data[i].resourceDemand + "'               class='input-medium2 '/>";
                    }

                    messageHtml += "    </div>" +
                        "    <div class='mainDivCs'>" +
                        "        <label class='control-label2'>优势：</label>";
                    if (data[i].advantage != null && data[i].advantage != "" && data[i].advantage.length > 14) {
                        messageHtml += "<textarea   rows='' cols='14' autoHeight=\"true\" >" + data[i].advantage + "</textarea>";
                    } else {
                        messageHtml += "        <input name=''" +
                            "               placeholder='优势' maxlength='50'" +
                            "    value='" + data[i].advantage + "'                 class='input-medium2 '/>";
                    }
                    messageHtml += "    </div>" +
                        "</div>" +
                        "<br/>"
                }
            }
            $("#mainDiv").html(messageHtml);
            $('textarea[autoHeight]').autoHeight();
        }
    })
}
